package com.link.base.base.activity.service;

import com.link.base.base.activity.dao.mybatis.mapper.FlowUpMapper;
import com.link.base.base.activity.model.FlowUp;
import com.link.base.base.common.dao.mybatis.mapper.AttachmentMapper;
import com.link.base.base.common.model.Attachment;
import com.link.base.base.opty.dao.mybatis.mapper.OptyItemMapper;
import com.link.base.base.opty.model.OptyItem;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 9:43
 */

@Service
public class FlowUpServiceImpl extends BasicServiceImpl<FlowUp> implements FlowUpService {

    @Resource
    private FlowUpMapper flowUpMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Resource
    private OptyItemMapper optyItemMapper;

    @Override
    public BasicMapper<FlowUp> getBasicMapper() {
        return flowUpMapper;
    }

    /**
     * 分页查询
     *
     * @param entity
     * @return List
     * @throws Exception
     */
    @Override
    public List<FlowUp> queryByExamplePage(FlowUp entity) throws Exception {

        List<FlowUp> list = super.queryByExamplePage(entity);
        if (!entity.getOnlyCountFlag()) {
            List<String> ids = new ArrayList<>();
            if (CollectionUtils.isEmpty(list)) {
                return list;
            }
            for (FlowUp fu : list) {
                ids.add(String.valueOf(fu.getId()));
                QueryParams head = new QueryParams();
                head.addFilter(new Filter("headId", String.valueOf(fu.getId())));
                head.setPageFlag(false);
                Attachment t = (Attachment) BasicModel.transformClass(Attachment.class, head);
                List<Attachment> attachmentList = attachmentMapper.queryByExamplePage(t);
                if (attachmentList != null && attachmentList.size() > 0) {
                    fu.setAttachmentList(attachmentList);
                }
            }
            Map<Long, String> amountMap = getAmountMap(ids);
            list.forEach(item -> {
                if (null == amountMap.get(item.getId())) {
                    item.setAmount(BigDecimal.ZERO);
                } else {
                    item.setAmount(new BigDecimal(amountMap.get(item.getId())));
                }
            });
        }
        return list;
    }

    /**
     * 获取key-图纸ID，value 总金额的MAP
     *
     * @author 黄正杨
     * @date 2019/4/23
     * @param ids
     * @return
     * @throws Exception
     */
    private Map<Long, String> getAmountMap(List<String> ids) throws Exception {
        Map<Long, String> amountMap = new HashMap<>();
        if (CollectionUtils.isEmpty(ids)) {
            return amountMap;
        }
        List<OptyItem> optyItems = optyItemMapper.selectAmountList(ids);
        if (CollectionUtils.isEmpty(optyItems)) {
            return amountMap;
        }
        optyItems.forEach(item -> amountMap.put(item.getBlueprintId(), item.getAmount()));
        return amountMap;
    }

    /**
     * 查询全部
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public List<FlowUp> queryAll(FlowUp entity) throws Exception {

        List<FlowUp> list = super.queryAll(entity);
        for (FlowUp fu : list) {
            QueryParams head = new QueryParams();
            head.addFilter(new Filter("headId", String.valueOf(fu.getId())));
            head.setPageFlag(false);
            Attachment t = (Attachment) BasicModel.transformClass(Attachment.class, head);
            List<Attachment> attachmentList = attachmentMapper.queryByExamplePage(t);
            if (attachmentList != null && attachmentList.size() > 0) {
                fu.setAttachmentList(attachmentList);
            }
        }
        return list;
    }

    @Override
    public List<FlowUp> queryProjByExamplePage(FlowUp entity) throws Exception {

        List<FlowUp> list = super.queryByExamplePage(entity);
        for (FlowUp fu : list) {
            QueryParams head = new QueryParams();
            head.addFilter(new Filter("headId", String.valueOf(fu.getId())));
            head.setPageFlag(false);
            Attachment t = (Attachment) BasicModel.transformClass(Attachment.class, head);
            List<Attachment> attachmentList = attachmentMapper.queryByExamplePage(t);
            if (attachmentList != null && attachmentList.size() > 0) {
                fu.setAttachmentList(attachmentList);
            }
        }
        return list;
    }

}
